<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>
<form class="layui-form">
    <input type="hidden" name="id" th:value="${menu.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">菜单名称</label>
        <div class="layui-input-inline">
            <input type="text" th:value="${menu.menuName}" name="menuName" lay-verify="required" lay-reqtext="菜单名称是必填项" placeholder="请输入菜单名称"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单url</label>
        <div class="layui-input-inline">
            <input th:value="${menu.url}" type="text" name="url" lay-verify="required" placeholder="请输入url" autocomplete="off"
                   class="layui-input">
        </div>
    </div>



    <div class="layui-inline">
        <label class="layui-form-label">选择菜单类型：</label>
        <div class="layui-input-inline">
            <select disabled="disabled" class="menuType" lay-filter="menuType" lay-verify="menuType" name="menuType" lay-search="">
                <option value="0">---请选择---</option>
                <option value="1" th:selected="${menu.parentId==0}">一级菜单</option>
                <option value="2" th:selected="${menu.parentId!=0}">二级菜单</option>
            </select>
        </div>
    </div>

    <div th:if="${menu.parentId!=0}" class="layui-inline hide"  >
        <label class="layui-form-label">选择父级菜单：</label>
        <div class="layui-input-inline">
            <select name="parentMemu" lay-verify="parentMemu" lay-search="" >
                <option value="0">---请选择---</option>
                <option  th:each="menu2 : ${Level1List}" th:value="${menu2.id}" th:text="${menu2.menuName}" th:selected="${menu2.id==menu.parentId}"></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1" id="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , $ = layui.$;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            menuType: function (value) {
                if(value==='0'){
                    return '请选择菜单类型';
                }
            }
            ,parentMemu: function (value) {
                if($(".menuType").val()==='2'&&value==='0'){
                    return '二级菜单请选择父级菜单';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听选中下拉框
        form.on('select(menuType)', function (data) {
            if(data.elem.value==='2'){
                $(".layui-inline.hide").show();
            }else{
                $(".layui-inline.hide").hide();
            }
            return false;
        });

        form.on('submit(demo1)', function (data) {
            $.ajax({
                url: '/updateMenu'
                , method: 'post'
                , data: data.field
                , success: function (res) {
                    layer.msg("修改成功");
                    setTimeout(function () {
                        window.parent.location.reload();//修改成功后刷新父界面
                    }, 1000);
                }
            });
            return false;
        });
    })
    ;
</script>
</html>